package com.yan.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.yan.controller.viewModel.login.UserModel;
import com.yan.controller.viewModel.looker.BookInfo;
import com.yan.util.CastMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.yan.util.PageConstant;
import com.yan.dao.mapper.SysAdminMapper;
import com.yan.dao.entity.Books;
import com.yan.dao.entity.SysAdmin;
import com.yan.dao.entity.Users;
import com.yan.service.SysAdminService;
import com.yan.vo.Page;

@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class SysAdminServiceImpl implements SysAdminService {

	@Autowired
	private SysAdminMapper sysAdminMapper;

	@Override
	public Page getBooks(BookInfo books) {
		Books book = (Books)CastMapper.CastTo(Books.class,books);
		Page page = new Page();
		if (book.getCurrentPage() == 0) {
			book.setCurrentPage(PageConstant.PAGE_NUM);
		}
		int start = (book.getCurrentPage() - 1) * PageConstant.PAGE_SIZE;
		book.setStart(start);
		List<Books> list = sysAdminMapper.getBooks(book);
		page.setList(list);
		int count = sysAdminMapper.getCount(book);
		int counPage = count / PageConstant.PAGE_SIZE;
		if (count % PageConstant.PAGE_SIZE != 0) {
			counPage++;
		}
		// 总页数
		page.setTotalPage(counPage);
		// 总记录数
		page.setCount(count);
		return page;
	}

	@Override
	public void addBooks(BookInfo book) {
		sysAdminMapper.addBooks((Books)CastMapper.CastTo(Books.class,book) );
	}

	@Override
	public BookInfo getBookById(Integer id) {
		return (BookInfo)CastMapper.CastTo(BookInfo.class, sysAdminMapper.getBookById(id));
	}

	@Override
	public void updateBooks(BookInfo book) {
		sysAdminMapper.updateBooks((Books) CastMapper.CastTo(Books.class,book));
	}

	@Override
	public void deleteBooks(Integer id) {
		sysAdminMapper.deleteBooks(id);
	}

	@Override
	public List<UserModel> getUsers(UserModel user) {
		List<Users> users =  sysAdminMapper.getUsers((Users)CastMapper.CastTo(Users.class,user));
		List<UserModel> userModels = new ArrayList<>(users.size());
		for(Users user1:users){
			userModels.add((UserModel) CastMapper.CastTo(UserModel.class,user1));
		}
		return userModels;
	}

	@Override
	public void addUsers(UserModel user) {
		sysAdminMapper.addUsers((Users)CastMapper.CastTo(Users.class,user));
	}

	@Override
	public UserModel getUserById(Integer id) {
		return (UserModel) CastMapper.CastTo(UserModel.class,sysAdminMapper.getUserById(id));
	}

	@Override
	public void updateUser(UserModel user) {
		sysAdminMapper.updateUser((Users)CastMapper.CastTo(Users.class,user));
	}

	@Override
	public void deleteUser(Integer id) {
		sysAdminMapper.deleteUser(id);
	}
	
	@Override
	public UserModel getSysAdmin(UserModel user) {
		SysAdmin sysAdmin = sysAdminMapper.getSysAdmin((Users) CastMapper.CastTo(Users.class,user));
		return (UserModel) CastMapper.CastTo(UserModel.class,sysAdmin);
	}

	@Override
	public int selectUsers(String code) {
		return sysAdminMapper.selectUsers(code);
	}

}
